import { fakeChartData ,fakeTableData} from '@/services/home';

export default {
  namespace: 'home',
  state: {
    visitorTup:{},
    chatCountTup:{},
    matchRateTup:{},
    unknownCountTup:{},
    hitCountTup:{},
    items:[],
    total:'',
    loading: false,
  },

  effects: {
    *fetch({payload,callback}, { call, put }) {
      const response = yield call(fakeChartData,payload);
      // let chatCountData = response.chatCountTup.chatCountData;
      // let chatCountDataYesterday = response.chatCountTup.yesterday;
      // let visitorData = response.visitorTup.visitorData;
      // let visitorDataYesterday = response.visitorTup.yesterday;
      // let hitCountTup = response.hitCountTup;
      // for(let i of chatCountData){
      //   i.y = parseInt(i.y)+1;
      // }
      // for(let i of visitorData){
      //   i.y = parseInt(i.y)+1;
      // }
      // for(let i of hitCountTup){
      //   i.y = parseInt(i.y)+1;
      // }
      // chatCountDataYesterday = parseInt(chatCountDataYesterday)+1;
      // visitorDataYesterday = parseInt(visitorDataYesterday)+1;
      // response.chatCountTup.chatCountData = chatCountData;
      // response.chatCountTup.yesterday =  chatCountDataYesterday;
      // response.visitorTup.visitorData = visitorData;
      // response.visitorTup.yesterday =  visitorDataYesterday;
      // response.hitCountTup = hitCountTup;
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fakeTableDataList({payload,callback},{call,put}){
      const response = yield call(fakeTableData,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield take({
        type: 'save',
        payload: response,
      });
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    clear() {
      return {
        visitorTup:{},
        chatCountTup:{},
        matchRateTup:{},
        unknownCountTup:{},
        hitCountTup:{},
        items:[],
        total:'',
      };
    }
  },
};
